!function() {
    var browser = {
        versions : function() {
            var u = navigator.userAgent;
            return {// 移动终端浏览器版本信息
                trident : u.indexOf('Trident') > -1, // IE内核
                presto : u.indexOf('Presto') > -1, // opera内核
                webKit : u.indexOf('AppleWebKit') > -1, // 苹果、谷歌内核
                gecko : u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, // 火狐内核
                mobile : !!u.match(/AppleWebKit.*Mobile.*/), // 是否为移动终端
                ios : !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), // ios终端
                android : u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, // android终端或者uc浏览器
                iPhone : u.indexOf('iPhone') > -1, // 是否为iPhone或者QQHD浏览器
                iPad : u.indexOf('iPad') > -1, // 是否iPad
                webApp : u.indexOf('Safari') == -1, // 是否web应该程序，没有头部与底部
                weixin : u.indexOf('MicroMessenger') > -1, // 是否微信
                qq : u.match(/\sQQ/i) == " qq" // 是否QQ
            };
        }(),
        language : (navigator.browserLanguage || navigator.language)
                .toLowerCase(),
        isPCBrowser : function() {
            if (this.versions.mobile || this.versions.ios
                    || this.versions.android || this.versions.iPhone
                    || this.versions.iPad) {
                return false;
            }
            return true;
        },
        isIos : function() {
            if (this.versions.ios || this.versions.iPhone || this.versions.iPad) {
                return true;
            }
            return false;
        },
        isAndroid : function() {
            if (this.versions.android) {
                return true;
            }
            return false;
        },
        isLandscape : function() {
            if (this.isPCBrowser()) {
                return true;
            }
            if (("orientation" in screen) && this.isAndroid()) {
                if (screen.orientation.angle === 90
                        || screen.orientation.angle === -90) {
                    return true;
                }
            }
            return false;
        }
    };

    /**
     * 初始化微信分享
     */
    function configWetcharShare() {
        if (typeof (wx) == "undefined") {
            return;
        }
        var link = 'http://fish-h5.9you-nj.cn/QPFish/index.html';
        link = window.location.href;
        wx.ready(function () {
            wx.onMenuShareTimeline({
                title: '惊！！！居然在海底世界做这种事...',
                link: link,
                imgUrl: "http://fish-h5.9you-nj.cn/fish.png",
                success: function () {
                   alert('success');
                },
                cancel: function () {
                   alert('cancel');
                }
            });

            wx.onMenuShareAppMessage({
                title: '惊！！！居然在海底世界做这种事...',
                desc: '各种鱼类游动姿态自然，场景变换丰富，为玩家营造了一个生动的海底狩猎场。玩家可以根据自身需求选择初级、中级或高级场，每桌可以四个玩家同时进行游戏从而增加游戏的趣味性和互动性',
                link: link,
                imgUrl: "http://fish-h5.9you-nj.cn/fish.png",
                trigger: function (res) {
                },
                success: function (res) {
                    alert('已分享');
                },
                cancel: function (res) {
                    alert('已取消');
                },
                fail: function (res) {
                    alert(JSON.stringify(res));
                }
            });
        });
    }

    function registerMessage() {
        // 添加自定义消息
        if (window.addEventListener) { // all browsers except IE before version 9
            window.addEventListener("message", onMessage, false);
        } else {
            if (window.attachEvent) { // IE before version 9
                window.attachEvent("onmessage", onMessage);
            }
        }
    }

    function onMessage(e) {
        var data = e.data || {};
        console.log("data : " + data);
        try{
            $("#pay_frame_div").remove();
        } catch (e) {
        }
        if (payWindow != null) {
            payWindow.close();
            payWindow = null;
        }
    }

    /**
     * 监控返回事件
     */
    function registerBackEvent() {
        if ('pushState' in history) {
            pushHistory();
            window.addEventListener("popstate", function(e) {
                if (dlgView.isShowing()) {
                    dlgView.close();
                    pushHistory();
                    return;
                }
                dlgView.showOkCancelDialog("信息", "确定要退出吗", function() {
                    if (isWeixin()) {
                        try {
                            WeixinJSBridge.call('closeWindow');
                        } catch (e) {
                        }
                    } else {
                        try{
                            window.location.href="about:blank";
                            window.close();
                        } catch(e) {
                            window.history.back();
                        }
                    }
                });
                pushHistory();
            }, false);
            function pushHistory() {
                var state = {
                    title : window.document.title,
                    url : window.location.href
                };
                window.history.pushState(state, window.document.title,
                        window.location.href);
            }
        }
    }

    function updateButtonState($button, colorState, allowDefault) {
        $button.on("touchstart", function(e) {
            var touch = e.originalEvent.targetTouches[0];
            $(this).attr("down", true);
            $(this).attr("x", touch.pageX);
            $(this).attr("y", touch.pageY);
            $(this).css({
                "background" : colorState.downcolor
            });
        });
        $button.on("touchmove", function(e) {
            if ($(this).attr("down") == "true") {
                if (!allowDefault) {
                    e.preventDefault();
                }
                var touch = e.originalEvent.targetTouches[0];
                var deltaX = touch.pageX - $button.attr("x");
                var deltaY = touch.pageY - $button.attr("y");
                var dis = Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2));
                if (dis > 5) {
                    $(this).css({
                        "background" : colorState.upcolor
                    });
                    $(this).attr("noclick", true);
                }
            }
        });
        $button.on("touchend", function() {
            $(this).removeAttr("down");
            $(this).css({
                "background" : colorState.upcolor
            });
        });

        $button.on("mousedown", function(e) {
            $(this).attr("down", true);
            $(this).attr("x", e.pageX);
            $(this).attr("y", e.pageY);
            $(this).css({
                "background" : colorState.downcolor
            });
        });
        $button.on("mousemove", function(e) {
            if ($(this).attr("down") == "true") {
                if (!allowDefault) {
                    e.preventDefault();
                }
                var deltaX = e.pageX - $button.attr("x");
                var deltaY = e.pageY - $button.attr("y");
                var dis = Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2));
                if (dis > 10) {
                    $(this).css({
                        "background" : colorState.upcolor
                    });
                    $(this).attr("noclick", true);
                }
            }
        });
        $button.on("mouseup", function() {
            $(this).removeAttr("down");
            $(this).css({
                "background" : colorState.upcolor
            });
        });
    }

    /**
     * 滚动条
     */
    var progress = {
        size : 80,
        view : null,
        intervalId : -1,
        show : function() {
            var $maskView = $("<div/>");
            $maskView.css({
                "width" : "100%",
                "height" : "100%",
                "position" : "fixed",
                "left" : "0",
                "top" : "0",
                "right" : "0",
                "bottom" : "0",
                "margin" : "0",
                "padding" : "0"
            });
            var $layout = $("<div/>");
            $layout.css({
                "width" : this.size + "px",
                "height" : this.size + "px",
                "line-height" : this.size + "px",
                "text-align" : "center",
                "position" : "absolute",
                "top" : "50%",
                "left" : "50%",
                "z-index" : "2",
                "border-radius" : "5px",
                "transform" : "translate(-50%, -50%)",
                "background" : "rgba(0, 0, 0, 0.8)"
            });
            var $progressBar = $("<div/>");
            $progressBar.css({
                "width" : this.size / 2 + "px",
                "height" : this.size / 2 + "px",
                "display" : "inline-block",
                "position" : "relative",
                "margin-top" : this.size / 4 - 2 + "px",
                "background" : "rgba(0, 0, 0, 0)",
                "border-radius" : this.size / 2 + "px",
                "border-top" : "2px solid white",
                "border-right" : "2px solid gray",
                "border-bottom" : "2px solid gray",
                "border-left" : "2px solid gray",
                "background" : "rgba(0, 0, 0, 0)",
            });
            $layout.append($progressBar);
            $maskView.append($layout);
            $("body").append($maskView);
            var degree = 0;
            var intervalId = setInterval(function() {
                degree += 10;
                if (degree > 360) {
                    degree = 0;
                }
                $progressBar.css("transform", "rotate(" + degree + "deg)");
            }, 1000 / 60);

            this.intervalId = intervalId;
            this.view = $maskView;
            $maskView.click(function() {
            });
        },
        close : function() {
            this.view.remove();
            clearInterval(this.intervalId);
        }
    };

    window.onorientationchange = function() {
        setTimeout(function(){
            dlgView.showByOrentation();
        }, 500);
    };

    window.dlgView = {
        dialogView : null,
        DlgType : {
            OkDialog : 1,
            OkCancelDialog : 2,
            listDialog : 3
        },
        showType : 0,
        internalData : {},
        showOkDialog : function(title, desc, onOk) {
            this.showType = this.DlgType['OkDialog'];
            this.internalData['title'] = title;
            this.internalData['desc'] = desc;
            this.internalData['onOk'] = onOk;
            var config = {};
            config['title'] = title;
            config['desc'] = desc;
            config['okbutton'] = "确定";
            config['buttons'] = 1;
            var that = this;
            this.dialogView = showDialogViewFunction(config, function() {
                that.close();
                if (onOk != null) {
                    onOk();
                }
            });
        },
        showOkCancelDialog : function(title, desc, onOk, onCancel) {
            this.showType = this.DlgType['OkCancelDialog'];
            this.internalData['title'] = title;
            this.internalData['desc'] = desc;
            this.internalData['onOk'] = onOk;
            this.internalData['onCancel'] = onCancel;
            var config = {};
            config['title'] = title;
            config['desc'] = desc;
            config['okbutton'] = "确定";
            config['cancelbutton'] = "取消";
            config['buttons'] = 2;
            var that = this;
            this.dialogView = showDialogViewFunction(config, function() {
                that.close();
                if (onOk != null) {
                    onOk();
                }
            }, function() {
                that.close();
                if (onCancel != null) {
                    onCancel();
                }
            });
        },
        isShowing : function() {
            return this.dialogView != null;
        },
        close : function() {
            this.dialogView.remove();
            this.showType = 0;
            this.dialogView = null;
        },
        /**
         * 使用说明
         * var data = [ {title : "微信支付", desc : "使用微信客户端支付"}, {title : "支付宝支付", desc : "使用支付宝客户端支付"}]
         * dlgView.showList("支付选择", data, function(i, item) {
         *     dlgView.showOkDialog("信息", JSON.stringify(item));
         * });
         */
        showList : function(title, data, onItemClick) {
            this.showType = this.DlgType['listDialog'];
            this.internalData['title'] = title;
            this.internalData['data'] = data;
            this.internalData['onItemClick'] = onItemClick;
            var that = this;
            this.dialogView = showListDialogFunction(title, data, function(
                    index, item) {
                that.close();
                if (onItemClick) {
                    onItemClick(index, item);
                }
            }, function() {
                that.close();
            });
        },
        showByOrentation : function() {
            if (this.dialogView != null) {
                this.dialogView.remove();
                if (this.showType == this.DlgType['OkDialog']) {
                    this.showOkDialog(this.internalData['title'],
                            this.internalData['desc'],
                            this.internalData['onOk']);
                } else if (this.showType == this.DlgType['OkCancelDialog']) {
                    this.showOkCancelDialog(this.internalData['title'],
                            this.internalData['desc'],
                            this.internalData['onOk'],
                            this.internalData['onCancel']);
                } else if (this.showType == this.DlgType['listDialog']) {
                    this.showList(this.internalData['title'],
                            this.internalData['data'],
                            this.internalData['onItemClick']);
                }
            }
        }
    };

    /**
     * 显示选择框 data = [{title : "微信支付", desc : "使用微信客户端支付"}, {title : "支付宝支付",
     * desc : "使用支付宝客户端支付"}]
     */
    function showListDialogFunction(title, data, onItemClick, onCancelClick) {
        var offsetWidth = $(window).width();
        var offsetHeight = $(window).height();
        var isLandscape = browser.isLandscape();
        var r = isLandscape ? 0 : 90;

        var curWidth = isLandscape ? offsetWidth : offsetHeight;
        var curHeight = isLandscape ? offsetHeight : offsetWidth;

        var $maskView = $("<div></div>");
        $("body").append($maskView);
        $maskView.css({
            "width" : "100%",
            "height" : "100%",
            "position" : "fixed",
            "left" : "0",
            "top" : "0",
            "right" : "0",
            "bottom" : "0",
            "margin" : "0",
            "padding" : "0",
            "z-index" : "1000",
            "background" : "rgba(64, 64, 64, 0.8)"
        });
        var $layout = $("<div/>");
        $maskView.append($layout);
        $layout.css({
            "background" : "white",
            // "border-radius" : "5px",
            // "width" : isLandscape ? curWidth * 0.75 : curWidth * 0.5,
            "min-width" : "10em",
            "position" : "absolute",
            "left" : "50%",
            "top" : "50%",
        });
        $layout.css({
            "-ms-transform" : "translate(-50%, -50%) rotate(" + r + "deg)",
            "-moz-transform" : "translate(-50%, -50%) rotate(" + r + "deg)",
            "-webkit-transform" : "translate(-50%, -50%) rotate(" + r + "deg)",
            "-o-transform" : "translate(-50%, -50%) rotate(" + r + "deg)",
            "transform" : "translate(-50%, -50%) rotate(" + r + "deg)"
        });
        var $title = $("<p/>");
        $title.css({
            "z-index" : "2",
            "height" : "3em",
            "width" : "100%",
            "line-height" : "3em",
            "padding" : "0",
            "margin" : "0",
            // "border-radius" : "5px 5px 0 0",
            "color" : "black",
            "font-weight" : "bold",
            "white-space" : "nowrap",
            "text-overflow" : "ellipsis",
            "-o-text-overflow" : "ellipsis",
            "overflow" : "hidden",
            "border" : "none",
            "cursor" : "default"
        });
        $title.text(title);
        $layout.append($title);
        var $line = $("<div/>");
        $line.css({
            "z-index" : "2",
            "height" : "1px",
            "background" : "gray",
            "margin" : "0",
            "padding" : "0",
            "width" : "100%",
        });
        $layout.append($line);
        var $content = $("<p/>");
        $content.css({
            "min-height" : "5em",
            "max-height" : curHeight * 0.5,
            "overflow-y" : "auto",
            "padding" : "0",
            "margin" : "0 1px 0 1px"
        });
        $layout.append($content);
        window.document.oncontextmenu = function() {
            return false;
        };
        var createItem = function(item) {
            var $item = $("<div/>");
            $item.css({
                "width" : "100%",
                "height" : "3em",
                "background" : "white",
                "cursor" : "default",
                "line-height" : "3em",
                "font-weight" : "bold",
                "color" : "black"
            });
            updateButtonState($item, {
                "downcolor" : "gray",
                "upcolor" : "white"
            }, true);
            $item.text(item['title']);
            return $item;
        };
        var totallen = data.length;
        for (var i = 0; i < totallen; i++) {
            var item = createItem(data[i]);
            (function(index) {
                item.click(function() {
                    if ($(this).attr("noclick") == "true") {
                        $(this).removeAttr("noclick");
                        return;
                    }
                    if (onItemClick != null) {
                        onItemClick(index, data[index]);
                    }
                });
            })(i);
            $content.append(item);
            if (i != totallen - 1) {
                $content.append($line.clone());
            }
        }
        $layout.append($line.clone());
        var $cancel = $title.clone();
        $cancel.text("取消");
        $cancel.css({
            "color" : "blue"
        });
        updateButtonState($cancel, {
            "downcolor" : "gray",
            "upcolor" : "white"
        }, false);
        $layout.append($cancel);
        $cancel.click(function() {
            if (onCancelClick != null) {
                onCancelClick();
            }
        });
        return $maskView;
    }

    function showDialogViewFunction(config, okFun, cancelFun) {
        var offsetWidth = $(window).width();
        var offsetHeight = $(window).height();
        var isLandscape = browser.isLandscape();
        var r = isLandscape ? 0 : 90;

        var curWidth = isLandscape ? offsetWidth : offsetHeight;
        var curHeight = isLandscape ? offsetHeight : offsetWidth;

        var $maskView = $("<div></div>");
        $("body").append($maskView);
        $maskView.css({
            "width" : "100%",
            "height" : "100%",
            "position" : "fixed",
            "left" : "0",
            "top" : "0",
            "right" : "0",
            "bottom" : "0",
            "margin" : "0",
            "padding" : "0",
            "z-index" : "1000",
            "background" : "rgba(64, 64, 64, 0.8)"
        });
        var $layout = $("<div/>");
        $maskView.append($layout);
        $layout.css({
            "background" : "white",
            // "border-radius" : "5px",
            "width" : isLandscape ? curWidth * 0.5 : curWidth * 0.5,
            "position" : "absolute",
            "left" : "50%",
            "top" : "50%",
        });
        $layout.css({
            "-ms-transform" : "translate(-50%, -50%) rotate(" + r + "deg)",
            "-moz-transform" : "translate(-50%, -50%) rotate(" + r + "deg)",
            "-webkit-transform" : "translate(-50%, -50%) rotate(" + r + "deg)",
            "-o-transform" : "translate(-50%, -50%) rotate(" + r + "deg)",
            "transform" : "translate(-50%, -50%) rotate(" + r + "deg)"
        });
        var $title = $("<p/>");
        $title.css({
            "z-index" : "2",
            "height" : "3em",
            "width" : "100%",
            "line-height" : "3em",
            "padding" : "0",
            "margin" : "0",
            // "border-radius" : "5px 5px 0 0",
            "color" : "black",
            "font-weight" : "bold",
            "white-space" : "nowrap",
            "text-overflow" : "ellipsis",
            "-o-text-overflow" : "ellipsis",
            "overflow" : "hidden",
            "border" : "none"
        });
        $title.text(config['title']);
        $layout.append($title);
        var $line = $("<div/>");
        $line.css({
            "z-index" : "2",
            "height" : "1px",
            "background" : "gray",
            "margin" : "0",
            "padding" : "0",
            "width" : "100%",
        });
        $layout.append($line);
        var $content = $("<p/>");
        $content.css({
            "min-height" : "3em",
            "line-height" : "3em",
            "max-height" : curHeight * 0.5,
            "word-break" : "break-all",
            "overflow-y" : "auto",
            "padding" : "0 5px 0 5px",
            "margin" : "0 1px 0 1px"
        });
        $content.text(config['desc']);
        $layout.append($content);
        var $okButton = $("<p/>");
        $okButton.css({
            "width" : config['buttons'] == 1 ? "100%" : "50%",
            "height" : "3em",
            "line-height" : "3em",
            // "border-radius" : config['buttons'] == 1 ? "0 0 5px 5px" : "0 0 0 5px",
            "border" : "none",
            "background" : "rgba(0, 0, 0, 0)",
            "-moz-user-select" : "none",
            "-webkit-user-select" : "none",
            "-ms-user-select" : "none",
            "user-select" : "none",
            "color" : "black",
            "margin" : "0",
            "font-weight" : "bold",
            "padding" : "0",
            "cursor" : "default",
            "display" : "inline-block"
        });
        $okButton.text(config['okbutton']);
        updateButtonState($okButton, {
            "downcolor" : "gray",
            "upcolor" : "white"
        });
        $okButton.click(function() {
            // 滑动时设置不触发点击属性
            if ($(this).attr("noclick") == "true") {
                $(this).removeAttr("noclick");
                return;
            }
            okFun();
            // 恢复上下文菜单
            window.document.oncontextmenu = function() {
                return true;
            };
        });

        $cancelButton = $okButton.clone();
        $cancelButton.text(config['cancelbutton']);
        $cancelButton.css({
            // "border-radius" : "0 0 5px 0"
        });
        updateButtonState($cancelButton, {
            "downcolor" : "gray",
            "upcolor" : "white"
        });
        $cancelButton.click(function() {
            // 滑动时设置不触发点击属性
            if ($(this).attr("noclick") == "true") {
                $(this).removeAttr("noclick");
                return;
            }
            cancelFun();
            // 回复上下文菜单
            window.document.oncontextmenu = function() {
                return true;
            };
        });

        $layout.append($line.clone());
        $layout.append($okButton);
        if (config['buttons'] > 1) {
            $layout.append($cancelButton);
        }
        var $vline = $("<div/>");
        $vline.css({
            "z-index" : "2",
            "height" : "3em",
            "background" : "gray",
            "margin" : "0",
            "padding" : "0",
            "width" : "1px",
            "position" : "absolute",
            "display" : "inline-block",
            "left" : "50%"
        });
        if (config['buttons'] > 1) {
            $layout.append($vline);
        }
        // 禁用上下文菜单
        window.document.oncontextmenu = function() {
            return false;
        };
        return $maskView;
    }

    /**
     * 判断是否是微信浏览器
     * 
     * @returns {Boolean}
     */
    function isWeixin() {
        var ua = navigator.userAgent.toLowerCase();
        if (ua.match(/MicroMessenger/i) == "micromessenger") {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断是否是可玩平台
     */
    function isKeWanPlatform() {
        return false;
    }

    function onBridgeReady(appId, timeStamp, nonceStr, packageExt, signType,
            paySign) {
        WeixinJSBridge.invoke('getBrandWCPayRequest', {
            "appId" : appId, // 公众号名称，由商户传入
            "timeStamp" : timeStamp, // 时间戳，自1970年以来的秒数
            "nonceStr" : nonceStr, // 随机串
            "package" : packageExt, // 扩展字段
            "signType" : signType, // 微信签名方式
            "paySign" : paySign
        // 微信签名
        }, function(res) {
            // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回 ok，但并不保证它绝对可靠。
            if (res.err_msg == "get_brand_wcpay_request:ok") {
                onPayResult(PayResult.SUCCESS, /* res.err_desc */"支付成功");
            } else if (res.err_msg == "get_brand_wcpay_request:fail") {
                onPayResult(PayResult.FAILURE, res.err_desc);
            } else if (res.err_msg == "get_brand_wcpay_request:cancel") {
                onPayResult(PayResult.CANCEL, "用户取消支付");
            }
        });
    }

    function execPay(appId, timeStamp, nonceStr, packageExt, signType, paySign) {
        if (typeof WeixinJSBridge == "undefined") {
            if (document.addEventListener) {
                document.addEventListener('WeixinJSBridgeReady', onBridgeReady,
                        false);
            } else if (document.attachEvent) {
                document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
            }
            onPayResult(PayResult.FAILURE, "请在微信浏览器中支付");
        } else {
            onBridgeReady(appId, timeStamp, nonceStr, packageExt, signType,
                    paySign);
        }
    }

    function preparePay(data) {
        try {
            var jobj = null;
            if (typeof (data) == "string") {
                jobj = eval("(" + data + ")");
            } else {
                jobj = data;
            }
            var result = jobj["resultStatus"];
            if ("SUCCESS" == result) {
                var data = jobj["data"];
                var appId = data["appId"];
                var timeStamp = data["timeStamp"];
                var nonceStr = data["nonceStr"];
                var packageExt = data["packageExt"];
                var signType = data["signType"];
                var paySign = data["paySign"];
                execPay(appId, timeStamp, nonceStr, packageExt, signType,
                        paySign);
            } else {
                var error = jobj["data"];
                onPayResult(PayResult.FAILURE, error);
            }
        } catch (e) {
            onPayResult(PayResult.FAILURE, "解析支付数据失败");
        }
    }

    function requestWxPayArgs(payinfo) {
        // var url = "http://106.14.185.49:8080/wxpay/request_payinfo.jsp";
        var url = "http://wap.9you-nj.cn/wap/wx/apply";
        progress.show();
        $.ajax({
            url : url,
            data : payinfo,
            type : "POST",
            success : function(data) {
                progress.close();
                preparePay(data);
            },
            error : function() {
                progress.close();
                onPayResult(PayResult.FAILURE, "请求支付数据失败");
            }
        });
    }

    /**
     * 通知支付结果
     * 
     * @param success
     * @param desc
     */
    function onPayResult(success, desc) {
        if (window.JYPAY.callback) {
            window.JYPAY.callback(success, desc);
        }
    }

    function checkPayInfo(payinfo) {
        if (payinfo == null) {
            onPayResult(PayResult.FAILURE, "支付参数必须传");
            return false;
        }
        if (payinfo.subject == null || payinfo.subject.trim() == ""
                || payinfo.subject.length > 128) {
            onPayResult(PayResult.FAILURE, "支付描述不能为空，且长度不能超过128");
            return false;
        }
        if (payinfo.amount == null
                || !/^[0-9]*[1-9][0-9]*$/.test(payinfo.amount)) {
            onPayResult(PayResult.FAILURE, "支付金额必须为正整数");
            return false;
        }
        return true;
    }

    function showPayResult (result, desc) {
        var str = null;
        if (result == PayResult.SUCCESS) {
            str = "支付成功";
        } else if (result == PayResult.FAILURE) {
            str = "支付失败";
        } else {
            str = "支付取消";
        }
        dlgView.showOkDialog(str, desc);
    }

    /**
     * 支付结果
     */
    window.PayResult = {
        SUCCESS : 0,
        FAILURE : 1,
        CANCEL : 2
    };

    // 定义sdk
    function JYPay() {
        // 回调函数
        this.callback = null;
    }

    JYPay.prototype.init = function() {
    };

    // 支付
    JYPay.prototype.pay = function(payinfo, callback) {
        this.callback = callback;
        if (checkPayInfo(payinfo)) {
            if (isWeixin()) {
                requestWxPayArgs(payinfo);
            } else if (isKeWanPlatform()) {
                var gid = ""; // 平台方的游戏id
                var sid = ""; // 平台方游戏的区服id
                var price = payinfo.amount; // 充值金额数目
                var username = ""; // 平台方用户名称
                var ext = ""; // 类似透传参数
                var sign = ""; // 签名md5
                var payUrl = "http://api.5971.com/pay";
                var qargs = "gid=" + gid + "&username=" + username + "&sid=" + sid + "&price=" + price + "&ext=" + ext + "&sign=" + sign;
                payUrl += "?";
                payUrl += qargs;
                payWindow = window.open(payUrl);
            } else {
                var h = "channel=" + payinfo.channel + "&appid="
                        + payinfo.appid + "&app_uid=" + payinfo.app_uid
                        + "&itemid=" + payinfo.itemid + "&subject="
                        + payinfo.subject + "&amount=" + payinfo.amount
                        + "&signtype=MD5&sign=" + payinfo.sign;
                var jubaoUrl = window.clientConfig.payUrl + "?" + h;
                payWindow = window.open(jubaoUrl);
            }
        }
    };

    window.JYPAY = new JYPay();
    registerBackEvent();
    configWetcharShare();
    registerMessage();

    // 支付弹出窗口
    window.payWindow = null;

    window.execPayItem = function(payinfo) {
        window.JYPAY.pay(payinfo, function(result, desc) {
            showPayResult(result, desc);
        });
    };
}();